const express = require('express')
const app = express()
const request = require('request');
const cheerio = require('cheerio')
const iconv = require('iconv-lite');
const fs = require('fs');

const port = 3000
app.use(express.static('views'))

// 爬取接口
app.get('/get/:year', (req, res) => {
  let { year } = req.params
  request.get(`https://fangjia.51240.com/${year}__fangjia/`, (err, response, body) => {
    if(err) return res.json({ err })
    return res.json( { 
      year,
      holiday: operation(body, year) 
    })
  })
  
})
// 时间过滤方法
Date.prototype.format = function() {
  var s = '';  
  var mouth = (this.getMonth() + 1)>=10?(this.getMonth() + 1):('0'+(this.getMonth() + 1));  
  var day = this.getDate()>=10?this.getDate():('0'+this.getDate());  
  // s += this.getFullYear() + '-'; // 获取年份。  
  s += mouth + "-"; // 获取月份。  
  s += day; // 获取日。  
  return (s); // 返回日期。  
}; 
// 获取时间区间
const getAllDate = (begin, end) => {
  var arr = [];
  var ab = begin.split("-");
  var ae = end.split("-");
  var db = new Date();
  db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
  var de = new Date();
  de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
  var unixDb = db.getTime() - 24 * 60 * 60 * 1000;
  var unixDe = de.getTime() - 24 * 60 * 60 * 1000;
  if(unixDb >= unixDe)
  return [ new Date(parseInt(unixDe + 24 * 60 * 60 * 1000)).format() ]
  for (var k = unixDb; k <= unixDe;) {
      k = k + 24 * 60 * 60 * 1000;
      arr.push((new Date(parseInt(k))).format());
  }
  return arr;
}
// 处理一下爬取结果
const operation = (body, year) => {
  let $ = cheerio.load(body), 
      json = {}, 
      reg = /['年'|'日'|'一'|'二'|'三'|'四'|'五'|'六'|'星期'|'天'|'（'|'）'|'上班']/g,
      reg2 = /['年'|'月']/g,
      reg3 = /[(\u4e00-\u9fa5) | ')' | '(']/g ,
      data = {}
  $('#main_content table tr').each( (i, tr) => {
    if(i > 0){
      let tds = $(tr).find('td')
      json[tds.eq(0).text()] = {
        date: tds.eq(1).text().replace(reg, ''),
        trim: tds.eq(2).text().replace(reg, ''),
        long: tds.eq(3).text().replace(reg, '')
      }
    }
  })

  for(key in json){
    let m = json[key].date.split('~').map(i => year + '-' + i)
    let fmtM = m.map(i => i.replace(reg2, '-').replace(reg3, ''))
    getAllDate(fmtM[0], fmtM[1] || fmtM[0]).forEach(i => {
      data[ i.split('-')[0] ] ? data[ i.split('-')[0] ][ i.split('-')[1] ] = 1 : data[ i.split('-')[0] ] = { [ i.split('-')[1] ]: 1 }
    })
    let t = json[key].trim.split('、')
    let fmtT = t.map(i => i.replace(reg2, '-').replace(reg3, ''))
    fmtT.forEach(i => {
      let m0 = i.split('-')[0] < 10 ? '0' + i.split('-')[0] : i.split('-')[0],
          d0 = i.split('-')[1] < 10 ? '0' + i.split('-')[1] : i.split('-')[1];
          if(m0 && d0){
            data[m0] ? data[m0][d0] = 2 : data[m0] = {  [d0]: 2 }
          }
    })
  }
  fs.writeFile(`./views/holidays/${year}.json`, JSON.stringify(data), err => {
      if(err) return console.log(err)
      console.log(`写入 ${year}.json 成功！`)
  })
  return data
}

app.listen(port, () => console.log(`server listening on port ${port}!`))